package com.changgou.web.gateway;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@SpringBootApplication
@EnableEurekaClient
public class WebGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebGatewayApplication.class,args);

    }

    /**
     * 创建用户的唯一的标识，使用ip作为用户的唯一的标识，来根据ip进行限流
     * @return
     */
    @Bean(name = "ipKeyResolver")
    public KeyResolver userKeyResolver(){
      return new KeyResolver() {
          @Override
          public Mono<String> resolve(ServerWebExchange exchange) {
              String ip = exchange.getRequest().getRemoteAddress().getHostString();
              System.out.println("用户的ip"+ip);
              return Mono.just(ip);
          }
      };
    }
}
